<?php
namespace app\index\common\controller;

use think\Controller;
use app\common\model\Nav as NavModel;
use app\common\model\Web as WebModel;
use app\common\model\FriendshipLink as FriendshipLinkModel;
use think\facade\Session;

class Common extends Controller
{

    /**
     * 初始化
     *
     * @return \think\Response
     */
    public function initialize()
    {
        // 获取分类编号
        $classify_id = request()->param('classify_id');
        // 设置有效分类编号
        $isClassifyId = array(7, 8);
        // 判断 获取当前控制器
        $nav = $classify_id && in_array($classify_id, $isClassifyId) ? NavModel::getByController(request()->controller())->getById($classify_id) : NavModel::getByController(request()->controller());
        // 获取基本信息
        $web = WebModel::get(1);
        // 获取全部导航栏
        $navs = NavModel::where('status', 1)->where('parent_id', 0)->order('sorting')->all();
        // 循环
        foreach ($navs as $index => $item) {
            // 获取子分类
            $childrens = NavModel::where('parent_id', $item['id'])->order('sorting')->all();

            // 设置子分类
            $navs[$index]['childrens'] = $childrens;
        }
        // 获取全部友情链接数据
        $friendshipLinks = FriendshipLinkModel::where('status', 1)->order('sorting')->all();

        // 模板变量赋值
        $this->assign(['nav' => $nav, 'web' => $web, 'navs' => $navs, 'friendshipLinks' => $friendshipLinks]);
    }
}